//
//  MySegButtonView.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/4/21.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "MySegButtonView.h"

#define btnSpace [UIView getWidth:30.0f]
#define btnWdith [UIView getWidth:120.0f]
#define grayViewheight 18.0f
@interface MySegButtonView ()
{
    UIButton *lastBtn;
}
@end

@implementation MySegButtonView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame: frame];
    if (self) {
//        [self addbtn];
    }
    return self;
}


- (void)setTitleArr:(NSArray *)titleArr{
    _titleArr = titleArr;
//    CGFloat btnW = (self.width - btnSpace * (titleArr.count + 1)) / titleArr.count;
    for (int i = 0; i< titleArr.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnSpace + (btnWdith + btnSpace) * i, 0, btnWdith, self.height - 20 )];
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
      
        btn.titleLabel.font = [ViewTool getFont:14.0f];;
        
        
        if (i == 0) {
            [btn setBackgroundImage:[UIImage imageNamed:@"Btn_Normal_Xiaoquguanli"] forState:UIControlStateNormal];
            btn.selected = YES;
            btn.enabled = NO;
            lastBtn = btn;
            [btn setTitleColor:UIColorFromRGB(0xff7703) forState:UIControlStateNormal];
        }else{
            btn.selected = NO;
            [btn setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateNormal];
        }
       
        
//        btn.layer.cornerRadius = 20;
//        btn.layer.masksToBounds = YES;
//        btn.layer.shouldRasterize =YES;
//        btn.backgroundColor = [UIColor orangeColor];
        [self addSubview:btn];
//        btn.tag = 7867 + i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - grayViewheight, self.width, grayViewheight)];
    backView.backgroundColor = UIColorFromRGB(0xf8f8fa);
    
    UIView *line1 = [ViewTool getLineViewWith:CGRectMake(0, 0, backView.width, 1) withBackgroudColor:UIColorFromRGB(0xe6e7eb)];
    [backView addSubview:line1];
    
    UIView *line2 = [ViewTool getLineViewWith:CGRectMake(0, backView.height - 1, backView.width, 1) withBackgroudColor:UIColorFromRGB(0xe6e7eb)];
    [backView addSubview:line2];
    [self addSubview:backView];
    
    
}

- (void)clickBtn:(UIButton *)btn
{
    if (lastBtn != btn) {
        [lastBtn setBackgroundImage:nil forState:UIControlStateNormal];
        [lastBtn setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateNormal];
        lastBtn.selected = NO;
        [btn setBackgroundImage:[UIImage imageNamed:@"Btn_Normal_Xiaoquguanli"] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0xff7703) forState:UIControlStateNormal];
        lastBtn.enabled = YES;
        btn.enabled = NO;
        lastBtn = btn;
        btn.selected = YES;
       
    }
    if(self.block){
        self.block(btn.tag , btn.currentTitle);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
